<template>
  <el-container class="model_index">
    <el-main class="model-tree" style="padding-bottom:10;padding-top:10">
      <el-card shadow="never" class="model-tree-left">
        <span style="font-size:14px;font-weight: 700;">组织架构</span>
        <el-tree
          ref="tree"
          class="treeitems"
          :data="departmentTreeList"
          :props="defaultProps"
          :default-expanded-keys="defaultList"
          :highlight-current="true"
          :check-on-click-node="true"
          :expand-on-click-node="false"
          default-expand-all
          node-key="id"
          style="margin-top: 10px;"
          :current-node-key="currentNodekey"
          @node-click="handleNodeClick"
        />
      </el-card>
      <div class="model-tree-right">
        <el-card shadow="never" class="tableCard">
          <vxe-toolbar :refresh="{ query: getList1 }">
            <template v-slot:buttons>
              <el-button type="primary" icon="el-icon-plus" @click="openDialog('', 1, false)">新增</el-button>
            </template>
          </vxe-toolbar>
          <vxe-table
            ref="xTree"
            text-algin="center"
            border
            stripe
            resizable
            highlight-hover-row
            row-id="id"
            :loading="loading"
            :data="list"
            :auto-resize="true"
            :height="$store.getters.searchRow"
          >
            <vxe-table-column field="code" width="120" title="职务代码" />
            <vxe-table-column field="name" width="200" title="职务名称" />
            <vxe-table-column field="departmentName" width="150" title="所属部门" />
            <vxe-table-column field="jobName" width="150" title="职务级别" />
            <vxe-table-column field="remark" min-width="200" title="备注" />
            <vxe-table-column field="publishStatus" min-width="80" align="center" title="状态">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.publishStatus" :inactive-value="0" :active-value="1" active-text inactive-text @change="changeState(scope)" />
              </template>
            </vxe-table-column>
            <vxe-table-column width="150" align="center" title="操作" fixed="right">
              <template slot-scope="scope">
                <el-button v-if="scope.row.publishStatus == 0" size="mini" type="text" @click="openDialog(scope.row.id, 2, false)">编辑</el-button>
                <el-button size="mini" type="text" @click="openDialog(scope.row.id, 2, true)">查看</el-button>
                <el-button v-if="scope.row.publishStatus == 0" size="mini" type="text" @click="delOrgPosition(scope.row.id)">删除</el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
        </el-card>
      </div>
    </el-main>
    <el-dialog v-if="dialogFormVisible" width="45%" :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="params" label-position="right" :disabled="title === '查看'" :rules="rules" label-width="7rem">
        <el-row class="mb16">
          <el-col :span="12">
            <el-form-item label="职务代码" prop="code">
              <el-input v-model="params.code" :disabled="disabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务名称" prop="name">
              <el-input v-model="params.name" :disabled="disabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务级别" prop="jobId">
              <el-select v-model="params.jobId" :disabled="disabled">
                <el-option v-for="item in orgJobList " :key="item.name" :value="item.id + ''" :label="item.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="顺序号" prop="sort">
              <el-input v-model="params.sort" :disabled="disabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb16">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="params.remark" type="textarea" :disabled="disabled" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="!disabled" type="primary" @click="editOradd">确 定</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import { getJobList, getPositionList, addPosition, modifyPosition, viewPosition, delPosition, changePositionStatus } from '@/api/org/orgSetting'
import { getEffectDepartmentListTree } from '@/api/org/organize'
export default {
  data() {
    return {
      orgJobList: [],
      dialogFormVisible: false,
      title: '',
      queryParams: {
        departmentId: 0,
        code: '',
        name: ''
      },
      params: {
        code: '',
        name: '',
        jobId: '',
        sort: 99,
        remark: ''
      },
      rules: {
        code: [{ required: true, message: '请输入职务代码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入职务名称', trigger: 'blur' }],
        jobId: [{ required: true, message: '请输入职务级别', trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'id'
      },
      currentNodekey: '',
      defaultList: [{
        id: 1
      }],
      departmentTreeList: [],
      loading: false,
      disabled: false,
      list: [],
      type: 1, // 1 添加 2修改
      operationType: 0 // 0 添加 1 注册
    }
  },
  created() {
    this.getDepartmentListTree()
    this.getOrgJobSelect()
  },
  methods: {
    getDepartmentListTree() {
      getEffectDepartmentListTree({}).then((response) => {
        this.currentNodekey = response[0].id
        this.departmentTreeList = [...response]
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.currentNodekey)
          this.handleNodeClick({ id: this.currentNodekey })
        })
      })
    },
    handleNodeClick(data) {
      // if (!data.children) {
      this.currentNodekey = data.id
      this.queryParams.departmentId = data.id
      this.getList()
      // }
    },
    // 获取行业列表
    getOrgJobSelect() {
      getJobList({}).then(res => {
        this.orgJobList = res
      })
    },
    getList1() {
      this.getList()
    },
    // 删除
    delOrgPosition(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPosition({ id }).then(res => {
          this.$message.success('删除成功!')
          this.getList()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 打开弹窗
    openDialog(id, type, bool) {
      this.disabled = bool
      this.operationType = type
      if (type === 2) {
        this.title = type === 2 ? '查看' : '编辑'
        viewPosition({ id }).then(response => {
          this.params = response
          this.dialogFormVisible = true
        })
      } else {
        this.title = '新增'
        this.params = {}
        this.params.departmentId = this.queryParams.departmentId
        this.params.sort = 99
        this.dialogFormVisible = true
      }
    },
    // 编辑或新增
    editOradd() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.operationType === 1) {
            addPosition(this.params).then(res => {
              this.$message.success('新增成功')
              this.dialogFormVisible = false
              this.getList()
            })
          } else {
            modifyPosition(this.params).then(res => {
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.getList()
            })
          }
        }
      })
    },
    getList() {
      getPositionList(this.queryParams).then(res => {
        this.list = res
      })
    },
    changeState(scope) {
      this.list[scope.seq - 1].publishStatus = scope.row.publishStatus === 1 ? 0 : 1
      changePositionStatus({
        id: scope.row.id,
        status: scope.row.publishStatus === 1 ? 0 : 1
      }).then(response => {
        this.list[scope.seq - 1].publishStatus = scope.row.publishStatus === 1 ? 0 : 1
        this.$message.success(scope.row.publishStatus === 0 ? '禁用成功' : '启用成功')
      })
    }
  }
}
</script>

<style>
.model_index {
  .model-tree {
    display: flex;
    .model-tree-left {
      margin-right: 20px;
      min-width: 250px;
      height: 640px;
      overflow-y: auto;
      .el-tree-node.is-current > .el-tree-node__content {
        background-color: #2F8DFB !important;
        color: white;
      }
    }
    .model-tree-right {
      width: calc(100% - 280px);
    }
  }
}
</style>
